package com.lan.mall.consumer.controller;

import com.lan.mall.common.utils.R;
import com.lan.mall.consumer.feign.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerController {

    @Value("${spring.application.name:consumer}")
    String appName;

    @Autowired
    private UserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/getUser")
    public R getUser(){
        String url = String.format("http://provider/user/getUser?appName=%S", appName);
        return restTemplate.getForObject(url, R.class);
    }

    @RequestMapping("/consumerHello")
    public R consumerHello(){
        R r = userService.providerHello();
        r.setData(r.getData() + " consumerHello");
        return r;
    }
}
